package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean register(User user) {
        // 检查用户名是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User existingUser = userMapper.selectOne(queryWrapper);
        
        if (existingUser != null) {
            return false; // 用户名已存在
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public User login(User user) {
        // 使用普通QueryWrapper代替Lambda查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean save(User user) {
        if (user.getPassword() == null) {
            user.setPassword("abc123456");
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updatePassword(Integer id, String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        return userMapper.updateById(user) > 0;
    }

    @Override
    public boolean update(User user) {
        return userMapper.updateById(user) > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        return userMapper.deleteBatchIds(ids) >= 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public Page<User> findPage(Integer pageNum, Integer pageSize, String search) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            wrapper.like("nick_name", search);
        }
        wrapper.eq("role", 2);
        return userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public Page<User> advancedSearch(Integer pageNum, Integer pageSize, 
                                    String search1, String search2, 
                                    String search3, String search4) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        
        if (StringUtils.isNotBlank(search1)) {
            wrapper.like("id", search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrapper.like("nick_name", search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrapper.like("phone", search3);
        }
        if (StringUtils.isNotBlank(search4)) {
            wrapper.like("address", search4);
        }
        
        wrapper.eq("role", 2);
        return userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }
} 